//
//  VVVisitorView.swift
//  Weibo
//
//  Created by 王惠平 on 2017/3/27.
//  Copyright © 2017年 Will Wang. All rights reserved.
//

import UIKit
import SnapKit

class VVVisitorView: UIView {

    //懒加载视图
    //旋转的视图
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //罩图
    private lazy var maskIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //主页
    private lazy var homeImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //访客视图信息
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.darkGray
        label.textAlignment = .center
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        //多行显示
        label.numberOfLines = 0
        
        return label
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton()
        
        //  添加点击事件
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
       
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        //手写代码拉伸
        let image =  UIImage(named:"common_button_white_disable")?.resizableImage(withCapInsets: UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1), resizingMode: UIImageResizingMode.stretch)
        
        button.setBackgroundImage(image, for: .normal)
        
        //取消高亮效果
        button.adjustsImageWhenHighlighted = false
        
        return button
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        
        //  添加点击事件
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
    
        //手写代码拉伸
        //UIImage(named:"common_button_white_disable").resizableImage(withCapInsets: UIEdgeInsets(top:1, left:1, bottom: 1,right:1), resizingMode:UIImageResizingMode.stretch)
        
        button.setBackgroundImage(UIImage(named:"common_button_white_disable")?.resizableImage(withCapInsets: UIEdgeInsets(top:1, left:1, bottom: 1,right:1), resizingMode:UIImageResizingMode.stretch), for: .normal)
        
        
        //取消高亮效果
        button.adjustsImageWhenHighlighted = false
        
        return button
    }()
    
    //设置闭包
    var loginClosure:(()->())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    //不支持xib
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件设置约束
    private func setupUI() {
        
        backgroundColor = UIColor(white: 237.0 / 255, alpha: 1)
        
        addSubview(cycleImageView)
        addSubview(maskIconImageView)
        addSubview(homeImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        cycleImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        maskIconImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(cycleImageView)
            
        }
        
        homeImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(maskIconImageView)
        }
        
        messageLabel.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(homeImageView)
            make.top.equalTo(cycleImageView.snp.bottom).offset(10)
            make.width.equalTo(224)
        }
        registerButton.snp.makeConstraints { (make) in
            
            make.leading.equalTo(messageLabel.snp.leading)
            make.top.equalTo(messageLabel.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 35))
            
        }
        
        loginButton.snp.makeConstraints { (make) in
            
            make.trailing.equalTo(messageLabel.snp.trailing)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        
        //startAnimation()
    }
    
    //开启旋转动画
    private func startAnimation() {
    
        //创建动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
    
        animation.toValue = 2 * M_PI
        
        animation.duration = 20
        
        animation.repeatCount = MAXFLOAT
        
        //动画失去焦点动画会释放,设置flase在动画执行次数内不会释放动画对象
        animation.isRemovedOnCompletion = false
        
        cycleImageView.layer.add(animation, forKey: nil)
    
    }
    
    //修改访客视图信息
    func updateVisitorInfo (imageName: String?, message: String?) {
        
        //多个可选类型的判断
        if let  imgName = imageName,let msg = message {
            
            //表示不是首页
            homeImageView.image = UIImage(named: imgName)
            messageLabel.text = msg
            
            cycleImageView.isHidden = true
        } else {
            
            //首页
            startAnimation()
        }
        
    }
    
    func loginAction() {
        
        loginClosure?()
        //print("登录")
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
